﻿Imports Negocio
Imports Utilitario
Public Class frmBusqArticulo

    Dim servicio1 As New ArticuloNegocio
    Dim servicio2 As New PedidoNegocio
    Dim almacen As String = ""
    Dim moneda As String = ""
    Dim famCodigo As String = ""

    Dim util As New Utiles

    Sub New(codAlmacen As String, codMoneda As String)

        ' Llamada necesaria para el diseñador.
        InitializeComponent()

        ' Agregue cualquier inicialización después de la llamada a InitializeComponent().
        Me.almacen = codAlmacen
        Me.moneda = codMoneda
    End Sub

    Sub New()

        ' Llamada necesaria para el diseñador.
        InitializeComponent()

        ' Agregue cualquier inicialización después de la llamada a InitializeComponent().

    End Sub

    Sub cargarSubFamilias(codLinea As String, codFamilia As String, combo As ComboBox)
        Dim ds As DataSet = servicio2.listarSubFamiliasXLineaXFamilia(codLinea, codFamilia)
        With combo
            .DataSource = ds.Tables(0)
            .ValueMember = "SFM_Codigo"
            .DisplayMember = "SFM_Descripcion"
        End With
    End Sub

    Sub cargarFamilias(codLinea As String, combo As ComboBox)
        Dim ds As DataSet = servicio2.listarFamiliasXLinea(codLinea)
        With combo
            .DataSource = ds.Tables(0)
            .ValueMember = "FAM_Codigo"
            .DisplayMember = "FAM_Descripcion"
        End With
    End Sub

    Sub cargarLineas(combo As ComboBox)
        Dim ds As DataSet = servicio2.listarLineas()
        With combo
            .DataSource = ds.Tables(0)
            .ValueMember = "LIN_Codigo"
            .DisplayMember = "LIN_Descripcion"
        End With
    End Sub

    Sub filtrarticulos()
        Dim ds As DataSet = servicio1.obtenerArticulosXFiltro(txtArticulo.Text.Trim, txtDescripcion.Text.Trim, txtProveedor.Text.Trim, almacen, famCodigo, CInt("0"), CDec("0"), "1")

        dgvArticulo.DataSource = ds.Tables(0)

        If ds.Tables(0).Rows.Count <> 0 Then
            dgvArticulo.Rows(0).Selected = False
        End If
    End Sub

    Private Sub btnBuscar_Click(sender As System.Object, e As System.EventArgs) Handles btnBuscar.Click
        famCodigo = IIf(cboLineas.SelectedValue.ToString = "0", "", cboLineas.SelectedValue.ToString) &
                    IIf(cboFamilias.SelectedValue.ToString = "0", "", cboFamilias.SelectedValue.ToString) &
                    IIf(cboSubFamilias.SelectedValue.ToString = "0", "", cboSubFamilias.SelectedValue.ToString)
        filtrarticulos()
    End Sub

    Private Sub frmBusqArticulo_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        cargarLineas(cboLineas)
        txtArticulo.Focus()
    End Sub

    Private Sub txtProveedor_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtProveedor.KeyDown
        Dim texto As String = txtProveedor.Text.Trim

        If e.KeyValue = Keys.Enter Then
            If texto = "" Then
                lblProveedor.Text = ""
            Else
                Dim ds As DataSet = servicio2.listarProveedores(texto)
                If ds.Tables(0).Rows.Count <> 0 Then
                    lblProveedor.Text = ds.Tables(0).Rows(0)("Descripcion").ToString.Trim
                    'focus ??
                End If
            End If
        ElseIf e.KeyValue = Keys.Add Then
            Dim ds As DataSet = servicio2.listarProveedores("")
            Dim prove As New frmProveedor(ds, 1)
            If prove.ShowDialog() = Windows.Forms.DialogResult.OK Then
                util.enfocar()
            End If
        End If
    End Sub

    Private Sub dgvArticulo_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvArticulo.CellClick
        Dim f As Integer = e.RowIndex

        If f > -1 Then
            'frmPedido.txtArticulo.Text = dgvArticulo.Item("clmCodigo", f).Value.ToString.Trim
            'frmPedido.lblArticulo.Text = dgvArticulo.Item("clmDescripcion", f).Value.ToString.Trim
            'frmPedido.precioSoles = CDec(dgvArticulo.Item("clmPrecio", f).Value.ToString.Trim)
            'frmPedido.precioDolares = CDec(dgvArticulo.Item("Column3", f).Value.ToString.Trim)
            'frmPedido.artPercepcion = dgvArticulo.Item("Column1", f).Value.ToString.Trim
            'frmPedido.artZMergencia = dgvArticulo.Item("Column2", f).Value.ToString.Trim
            'frmPedido.unidadMed = dgvArticulo.Item("clmMedida", f).Value.ToString.Trim

            frmMenu.formDocumento.txtArticulo.Text = dgvArticulo.Item("clmCodigo", f).Value.ToString.Trim
            DialogResult = Windows.Forms.DialogResult.OK
        End If
    End Sub

    Private Sub cboLineas_SelectedValueChanged(sender As Object, e As System.EventArgs) Handles cboLineas.SelectedValueChanged
        cargarFamilias(cboLineas.SelectedValue.ToString, cboFamilias)
    End Sub

    Private Sub cboFamilias_SelectedValueChanged(sender As Object, e As System.EventArgs) Handles cboFamilias.SelectedValueChanged
        cargarSubFamilias(cboLineas.SelectedValue.ToString, cboFamilias.SelectedValue.ToString, cboSubFamilias)
    End Sub

    Private Sub dgvArticulo_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles dgvArticulo.KeyDown
        If e.KeyValue = Keys.Enter Then
            frmMenu.formDocumento.txtArticulo.Text = dgvArticulo.Rows(dgvArticulo.CurrentRow.Index).Cells("clmCodigo").Value
            DialogResult = Windows.Forms.DialogResult.OK
        End If
    End Sub

End Class